package tcdx.uap.service.entities;

import tcdx.uap.common.utils.Lutils;

import java.util.*;
import java.util.stream.Collectors;

public class UserAction {
    public String user_id = "";
    public String staff_nm = "";
    public String action_type = "";
    public Date action_time;
    private List<Map> tableColumnConfigList = new ArrayList<>();
    //查看是否存在某个表的列配置，如果没有则要添加
    public boolean hasTabledColumnConfig(Integer table_id){
        return tableColumnConfigList.stream().filter(o->o.get("table_id").equals(table_id)).collect(Collectors.toList()).size()>0;
    }

    //查看是否存在某个表的列配置，如果没有则要添加
    public boolean hasTabledTriggerByConfig(Integer table_id){
        return tableColumnConfigList.stream().filter(o->o.get("trigger_by_datatable_id").equals(table_id)).collect(Collectors.toList()).size()>0;
    }

    public List<Map> getTableColumnConfig(Integer table_id){
        return tableColumnConfigList.stream().filter(o->o.get("table_id").equals(table_id)).collect(Collectors.toList());
    }


    public List<Map> getTableColumnDependConfig(Integer table_id, Map<String,Object> form){
        List<Map> l = new ArrayList<>();
        for(Map m : tableColumnConfigList){
            for(Map.Entry<String,Object> ent: form.entrySet()) {
                if (m.get("table_id").equals(table_id) && (
                        m.get("match_column1").equals(ent.getKey())
                        || m.get("match_column2").equals(ent.getKey())
                )) {
                    if(!form.containsKey(m.get("field")))
                        l.add(m);
                }
            }
        }
        return l;
    }

    public List<Map> getTableColumnTriggerConfig(Integer table_id, String column){
        List<Map> l = new ArrayList<>();
        for(Map m : tableColumnConfigList){
            if(m.get("match_from_datatable_id").equals(table_id) || m.get("match_from_column1").equals(column)
                    || m.get("match_from_column2").equals(column) || m.get("update_from_column").equals(column) ){
                l.add(m);
            }
        }
        return l;
    }

    //查看是否存在某个表的列配置，如果没有则要添加
    public List<Map> hasTableColumnConfigById(Integer id){
        return tableColumnConfigList.stream().filter(o->o.get("id").equals(id)).collect(Collectors.toList());
    }

    //添加表配置
    public void putTabledColumnConfig(List<Map> tableColumnConfig){
        for(Map m : tableColumnConfig){
            if(hasTableColumnConfigById((Integer)m.get("id")).size()==0){
                tableColumnConfigList.add(m);
            }
        }
    }

    //查看是否存在关系
    public Map isTableColumnDependent(Object table_id,String column){
        List <Map> find = tableColumnConfigList.stream().filter(o->o.get("table_id").equals(table_id)
                && (o.get("match_field1").equals(column) || o.get("match_field2").equals(column))
        ).collect(Collectors.toList());
        if(find.size()>0){
            return find.get(0);
        }
        else{
            return null;
        }
    }

    //查看是否存在关系
    public Map getTableMatchFromRelation(Object table_id,String column){
        List <Map> find = tableColumnConfigList.stream().filter(o->o.get("table_id").equals(table_id)
                && (o.get("match_field1").equals(column) || o.get("match_field2").equals(column))
        ).collect(Collectors.toList());
        if(find.size()>0){
            return find.get(0);
        }
        else{
            return null;
        }
    }

    public List<Map<String, Object>> updatedTableColumns = new ArrayList<>(); // update:table_id->column
    public boolean putUpdatedTableColumn(Integer table_id,String column){
        List<Map<String, Object>> l= updatedTableColumns.stream()
                .filter(o->o.get("table_id").equals(table_id)&&o.get("column").equals(column))
                .collect(Collectors.toList());
        if(l.size()>0){
            return false;
        }
        else{
            updatedTableColumns.add(Lutils.genMap("table_id", table_id, "column", column));
            return true;
        }
    }

    public UserAction(String action_type,Date action_time){
        this.action_type = action_type;
        this.action_time = action_time;
    }

    List<Map> recursion_trigger_list = new ArrayList<>(); //存储 key=table_name,value=column

    //判断是否已经操作过该字段
    public boolean addRecursionTrigger(String table_name, String column) {
        List<Map> found = recursion_trigger_list.stream().filter(o->o.get("table_name").equals("column")).collect(Collectors.toList());
        if(found.size()>0){
            return false;
        }
        else{
            recursion_trigger_list.add(Lutils.genMap(table_name, column));
            return true;
        }
    }
}

